package com.storeapp.ui;

import java.util.ArrayList;
import java.util.Iterator;

import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.MessageBox;
import org.eclipse.ui.part.ViewPart;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.List;
import org.eclipse.jface.util.IPropertyChangeListener;
import org.eclipse.jface.util.PropertyChangeEvent;
import org.eclipse.jface.viewers.ArrayContentProvider;
import org.eclipse.jface.viewers.ISelectionChangedListener;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.jface.viewers.ListViewer;
import org.eclipse.jface.viewers.SelectionChangedEvent;

import com.storeapp.entity.domain.Item;
import com.storeapp.service.DealOrderService;
import com.storeapp.util.SpringUtils;

import org.eclipse.swt.widgets.Text;

public class ItemEditView extends ViewPart {
	public static final String ID = "com.storeapp.ui.ItemEditView";
	private ListViewer lv;
	private java.util.List<Item> items;
	private Text totalPriceText;
	private Text itemNameText;
	private Text providerNameText;
	private Text guarenteeText;
	private Text discountText;
	private Text quantityText;
	private Text priceText;
	private DealOrderService orderService;
	private ArrayList<IPropertyChangeListener> listeners;

	class ViewLabelProvider extends LabelProvider {
		public String getText(Object obj) {
			Item item = (Item) obj;
			return item.getItemName() + "     " + item.getPrice() + "*"
					+ item.getQuantity();
		}
	}

	public ItemEditView() {
		this.items = new ArrayList<Item>();
		this.listeners = new ArrayList<IPropertyChangeListener>();
		this.orderService = (DealOrderService) SpringUtils
				.getApplicationContext().getBean("dealOrderService");
	}

	@Override
	public void createPartControl(Composite parent) {
		// TODO Auto-generated method stub
		FillLayout filllayout = new FillLayout(SWT.HORIZONTAL);
		filllayout.marginHeight = 10;
		filllayout.marginWidth = 10;
		parent.setLayout(filllayout);
		
		// item detail information group
		Group itemGroup = new Group(parent, SWT.BORDER | SWT.SHADOW_ETCHED_IN);
		itemGroup.setText("商品信息：");
		itemGroup.setLayout(new GridLayout(4, false));

		Label productLabel = new Label(itemGroup, SWT.NONE);
		productLabel.setText("商品：");
		productLabel.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, true,
				false, 5, 1));
		Combo productCombo = new Combo(itemGroup, SWT.NONE);
		productCombo.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true,
				false, 5, 1));

		Label label2 = new Label(itemGroup, SWT.SEPARATOR | SWT.HORIZONTAL);
		label2.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 5,
				1));
		Label itemNameLabel = new Label(itemGroup, SWT.NONE);
		itemNameLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false,
				false, 1, 1));
		itemNameLabel.setText("商品名称：");
		itemNameText = new Text(itemGroup, SWT.BORDER);
		itemNameText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true,
				false, 3, 1));

		Label providerNameLable = new Label(itemGroup, SWT.NONE);
		providerNameLable.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER,
				false, false, 1, 1));
		providerNameLable.setText("供货商名称：");
		providerNameText = new Text(itemGroup, SWT.BORDER);
		providerNameText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true,
				false, 3, 1));

		Label guarenteeLabel = new Label(itemGroup, SWT.NONE);
		guarenteeLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false,
				false, 1, 1));
		guarenteeLabel.setText("保修期：");
		guarenteeText = new Text(itemGroup, SWT.BORDER);
		guarenteeText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true,
				false, 3, 1));

		Label priceLabel = new Label(itemGroup, SWT.NONE);
		priceLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false,
				false, 1, 1));
		priceLabel.setText("单价：");
		priceText = new Text(itemGroup, SWT.BORDER);
		priceText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false,
				3, 1));

		Label discountLabel = new Label(itemGroup, SWT.NONE);
		discountLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false,
				false, 1, 1));
		discountLabel.setText("折扣：");
		discountText = new Text(itemGroup, SWT.BORDER);
		discountText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true,
				false, 3, 1));

		Label qutityLabel = new Label(itemGroup, SWT.NONE);
		qutityLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false,
				false, 1, 1));
		qutityLabel.setText("数量：");
		quantityText = new Text(itemGroup, SWT.BORDER);
		quantityText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true,
				false, 3, 1));

		new Label(itemGroup, SWT.NONE);
		Button addButton = new Button(itemGroup, SWT.NONE);
		addButton.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false,
				1, 1));
		addButton.setText("添加");
		addButton.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				Item item = new Item();
				item.setItemName(itemNameText.getText());
				item.setProvider(providerNameText.getText());
				item.setGuarentee(guarenteeText.getText());
				item.setQuantity(Integer.valueOf(quantityText.getText()));
				item.setPrice(Double.valueOf(priceText.getText()));
				item.setDiscount(Double.valueOf(discountText.getText()));

				items.add(item);
				lv.refresh();
			}
		});
		Button updateButton = new Button(itemGroup, SWT.NONE);
		updateButton.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true,
				false, 1, 1));
		updateButton.setText("修改");
		updateButton.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				IStructuredSelection selected = (IStructuredSelection) lv
						.getSelection();
				if (!selected.isEmpty()) {
					Item item = (Item) selected.getFirstElement();
					item.setItemName(itemNameText.getText());
					item.setProvider(providerNameText.getText());
					item.setGuarentee(guarenteeText.getText());
					item.setQuantity(Integer.valueOf(quantityText.getText()));
					item.setDiscount(Double.valueOf(discountText.getText()));
					item.setPrice(Double.valueOf(priceText.getText()));
					lv.refresh();
				}
			}
		});
		Button deletButton = new Button(itemGroup, SWT.NONE);
		deletButton.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true,
				false, 1, 1));
		deletButton.setText("删除");
		deletButton.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				IStructuredSelection selected = (IStructuredSelection) lv
						.getSelection();
				if (!selected.isEmpty()) {
					items.removeAll(selected.toList());
					lv.refresh();
				}
			}
		});
		
		// order detail information group
		Group orderGroup = new Group(parent, SWT.BORDER | SWT.SHADOW_OUT);
		orderGroup.setText("订单信息：");
		orderGroup.setLayout(new GridLayout(1, false));
		Label cutomerLabel = new Label(orderGroup, SWT.NONE);
		cutomerLabel.setText("客户：");
		Combo costomerCombo = new Combo(orderGroup, SWT.NONE);
		costomerCombo.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false,
				false, 1, 1));

		lv = new ListViewer(orderGroup, SWT.BORDER | SWT.V_SCROLL);
		lv.setContentProvider(new ArrayContentProvider());
		lv.setLabelProvider( new ViewLabelProvider() );
		lv.addSelectionChangedListener(new ISelectionChangedListener() {
			public void selectionChanged(SelectionChangedEvent arg0) {
				// TODO Auto-generated method stub
				if (!arg0.getSelection().isEmpty()) {
					IStructuredSelection selected = (IStructuredSelection) arg0
							.getSelection();
					Item item = (Item) selected.getFirstElement();
					itemNameText.setText(item.getItemName());
					providerNameText.setText(item.getProvider());
					guarenteeText.setText(item.getGuarentee());
					quantityText.setText(String.valueOf(item.getQuantity()));
					priceText.setText(String.valueOf(item.getPrice()));
					discountText.setText(String.valueOf(item.getDiscount()));
				}
			}

		});
		//development purpose
		setDefaultData();
		lv.setInput(items);
		List list = lv.getList();
		list.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, true, 1, 5));

		Label label1 = new Label(orderGroup, SWT.SEPARATOR | SWT.HORIZONTAL);
		label1.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1,
				1));

		Label totalPriceLabel = new Label(orderGroup, SWT.NONE);
		totalPriceLabel.setText("总计：");
		totalPriceText = new Text(orderGroup, SWT.BORDER);
		totalPriceText.setEnabled(false);
		totalPriceText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true,
				false, 1, 1));
		totalPriceText.setText("00.00");
		
		Button saveOrderButton = new Button(orderGroup, SWT.NONE);
		saveOrderButton.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true,
				false, 1, 1));
		saveOrderButton.setText("保存订单");
		saveOrderButton.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				orderService.saveDealOrder(Long.valueOf(1), items);
				MessageBox mb = new MessageBox(getSite().getShell());
				mb.setText("保存成功");
				notifyListeners();
				getSite().getWorkbenchWindow().getActivePage().findView(ItemEditView.ID).dispose();
			}
		});
		
		//add property listener to notify orderlist view to update list
		addPropertyChangeListener((IPropertyChangeListener) getSite().getWorkbenchWindow().getActivePage().findView(OrderList.ID));
	}
	
	// A public method that allows listener registration
	public void addPropertyChangeListener(IPropertyChangeListener listener) {
		if(!listeners.contains(listener))
			listeners.add(listener);
	}

	// A public method that allows listener registration
	public void removePropertyChangeListener(IPropertyChangeListener listener) {
		listeners.remove(listener);
	}
	
	void notifyListeners() {			
		// Post Invocation, inform listeners
		for (Iterator<IPropertyChangeListener> iter = listeners.iterator(); iter.hasNext();) {
			IPropertyChangeListener element = iter.next();
			element.propertyChange(new PropertyChangeEvent(this, "AddNewOrder" , null , null));
		}
	}

	@Override
	public void setFocus() {
		// TODO Auto-generated method stub
		lv.getControl().setFocus();
	}

	/*
	 * for develop and test purpose
	 */
	protected void setDefaultData() {
		for (int i = 1; i < 10; i++) {
			this.items.add(new Item("item" + i, "provider" + i, i + " year",
					(100 - i * 10.00) / 100, i, i * 10.00));
		}
	}

}
